package com.dzm.service.impl;

import com.dzm.entity.Advertisement;
import com.dzm.entity.Category;
import com.dzm.mapper.AdvertisementMapper;
import com.dzm.mapper.CategoryMapper;
import com.dzm.service.AdvertisementService;
import com.dzm.vo.SearchVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 作者：Dzm
 * 时间：2024-08-10-9:37
 * 功能：
 */

@Service
public class AdvertisementServiceImpl implements AdvertisementService {
    @Autowired
    private AdvertisementMapper advertisementMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page<Advertisement> search(Integer pageNum, Integer pageSize, SearchVO searchVO) {
        Page<Advertisement> page = PageHelper.startPage(pageNum, pageSize).doSelectPage(()->advertisementMapper.search(searchVO));

        for (Advertisement advertisement : page.getResult()) {
            Category category = categoryMapper.getById(advertisement.getCategoryId());
            advertisement.setCateName(category.getName());
        }

        return page;
    }

    @Override
    public void addAdvertisement(Advertisement advertisement) {
        advertisementMapper.addAdvertisement(advertisement);
    }

    @Override
    public void updateAdvertisement(Advertisement advertisement) {
        advertisementMapper.updateAdvertisement(advertisement);
    }

    @Override
    public Advertisement getById(Integer id) {
        Advertisement advertisement = advertisementMapper.getById(id);
        return advertisement;
    }

    @Override
    public void deleteByIds(String ids) {
        advertisementMapper.deleteByIds(ids);
    }
}
